package com.firstgroup.service.Impl;
import com.firstgroup.Utils.AdminUtil;
import com.firstgroup.Utils.ResponseResult;
import com.firstgroup.Utils.UserUtil;
import com.firstgroup.domain.Interviewers;
import com.firstgroup.domain.Page;
import com.firstgroup.domain.QuessionBank;
import com.firstgroup.domain.User;
import com.firstgroup.repository.AdminRepository;
import com.firstgroup.repository.InterviewersRepository;
import com.firstgroup.repository.QuestionBankRespository;
import com.firstgroup.repository.UserRepository;
import com.firstgroup.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminRepository admin;
    @Autowired
    private QuestionBankRespository question;
    @Autowired
    private InterviewersRepository interviewers;

    private static Logger logger = LoggerFactory.getLogger(AdminServiceImpl.class);
    private UserUtil userUtil = new UserUtil();
    private static AdminUtil adminUtil = new AdminUtil();
    @Resource
    UserRepository userRepository;
    //新增用户
    @Override
    public String addUser(User user) {
        boolean username = adminUtil.strLength(user.getUsername(), 4, 16);
        boolean password = adminUtil.strLength(user.getPassword(), 6, 18);
        User byName = admin.findByUserName(user.getUsername());
        //为空说明该用户不存在，添加添加
        if(byName==null){
                if(username&&password){
                    User save = admin.save(user);
                    logger.info("新增信息无误{}",user);
                    return "添加成功";
                }else{
                    logger.info("新增信息有误{}",user);
                    return "添加失败";
                }
        }else{
            return "该用户已存在";
        }
    }

    @Override
    public List<User> batchAddUser(List<User> list) {
        for (User user : list) {
            boolean username = adminUtil.strLength(user.getUsername(), 4, 16);
            boolean password = adminUtil.strLength(user.getPassword(), 6, 18);
            User byName = admin.findByUserName(user.getUsername());
            //为空说明该用户不存在，添加添加
            if (byName == null) {
                if (username && password) {
                    User save = admin.save(user);
                    logger.info("批量新增用户{}", save);
                } else {
                    logger.info("批量新增有误");
                }
            }else{
                logger.info("该用户已存在{}",user.getUsername());
                return null;
            }
        }
        return list;
    }
    //新增用户


    //删除
    @Transactional
    @Rollback(value = false)
    public int deleteUser(Integer id) {
        admin.deleteById(id);
        return 1;
    }

    @Transactional
    @Rollback(value = false)
    public int batchDeleteUser(Integer[] id) {
        int i =0;
        for (Integer integer : id) {
            admin.deleteById(integer);
            i++;
        }
        return i;
    }

    @Transactional
    @Rollback(value = false)
    public int deleteAllUser() {
        admin.deleteAll();
        return 1;
    }
    //删除

    //初始化密码
    @Transactional
    @Rollback(value = false)
    public int setPassword(Integer id) {
        return admin.updatePassword(id);
    }

    @Transactional
    @Rollback(value = false)
    public int batchSetPassword(Integer[] id) {
        int i = 0;
        for (Integer integer : id) {
            i += admin.updatePassword(integer);
        }
        return i;
    }

    @Transactional
    @Rollback(value = false)
    public int setAllPassword() {
        return admin.updateAllPassword();
    }
    //初始化密码

    //分页显示用户
    @Override
    public Page<User> queryAllUser(Integer pageNow, Integer pageSize) {
        Page<User> page = new Page<>();
        page.setPageTotalCount((int) admin.count());
        page.setPageNow(pageNow);
        page.setPageSize(pageSize);
        page.setPageTotal(page.getPageTotal());
        page.setStart((pageNow - 1) * pageSize);
        List<User> list = admin.findByUser(page.getStart(),pageSize);
        page.setList((ArrayList<User>) list);
        return page;
    }

    @Override
    public User updateUser(User user) {
        if(user.getUsername()!=null && user.getPassword()!=null){
            logger.info("新增用户{}",user);
            return admin.save(user);
        }else {
            return null;
        }
    }

    @Override
    public List<User> findByNameLike(String name) {
        if(adminUtil.strLength(name, 2, 7)){
            logger.info("模糊查询的用户姓名为{}",name);
            return admin.findByNameLike(name);
        }else {
            logger.info("模糊查询用户失败，用户姓名长度不匹配{}",name);
            return null;
        }
    }

    @Override
    public List<User> findByUsernameLike(String username) {
        if(adminUtil.isNull(username)){
            return admin.findByUsernameLike(username);
        }else {
            logger.info("該用户不存在{}",username);
            return null;
        }
    }

    //对题库的操作
    @Override
    public QuessionBank addQuestion(QuessionBank questionBank) {
        if(questionBank.getType()==null){
            logger.info("添加题库，种类为空");
            return null;
        }else{
            if(questionBank.getType().equals("笔试")){
                if(questionBank.getAnswer()==null){
                    logger.info("笔试答案为空");
                    return null;
                }else{
                    logger.info("添加笔试题成功{}",questionBank);
                    return question.save(questionBank);
                }
            }else{
                logger.info("添加面试题成功{}",questionBank);
                return question.save(questionBank);
            }
        }
    }

    @Override
    @Transactional
    @Rollback(value = false)
    public Integer deleteQuestion(int id) {
        return question.deleteById(id);
    }

    @Override
    @Transactional
    @Rollback(value = false)
    public Integer updateQuestion(int id, String questionName, String type, String answer) {
        if(type==null){
            logger.info("题目种类为空");
            return -1;
        }else{
            if(type.equals("笔试")){
                if(answer==null){
                    logger.info("笔试答案为空");
                    return -1;
                }else {
                    logger.info("更新笔试题库成功");
                    return question.updateQuession( id,questionName, type,answer);
                }
            }else{
                logger.info("更新面试题库成功");
                return question.updateQuession(id,questionName, type,answer);
            }
        }
    }

    //遍历题库 分页
    @Override
    public Page<QuessionBank> findQuestions(Integer pageNow, Integer pageSize) {
        Page<QuessionBank> page = new Page<>();
        page.setPageTotalCount((int) question.count());
        page.setPageNow(pageNow);
        page.setPageSize(pageSize);
        page.setPageTotal(page.getPageTotal());
        page.setStart((pageNow - 1) * pageSize);
        List<QuessionBank> all = question.findAll();
        page.setList((ArrayList<QuessionBank>) all);
        return page;
    }

    //根据类型遍历
    @Override
    public Page<QuessionBank> findByType(String type,Integer pageNow, Integer pageSize) {
        Page<QuessionBank> page = new Page<>();
        page.setPageTotalCount((int) question.queryCountByType(type));
        page.setPageNow(pageNow);
        page.setPageSize(pageSize);
        page.setPageTotal(page.getPageTotal());
        page.setStart((pageNow - 1) * pageSize);
        List<QuessionBank> all = question.findByQuestionType(type,page.getStart(),pageSize);
        page.setList((ArrayList<QuessionBank>) all);
        return page;
    }

    //根据id查找题库信息
    @Override
    public QuessionBank findById(int id) {
        return question.findById(id);
    }

    //查找面试地库
    @Override
    public Page<QuessionBank> findByQuestionName(String name,Integer pageNow, Integer pageSize) {
        if(adminUtil.isNull(name)){
            Page<QuessionBank> page = new Page<>();
            page.setPageTotalCount(question.queryCountByType("面试"));
            page.setPageNow(pageNow);
            page.setPageSize(pageSize);
            page.setPageTotal(page.getPageTotal());
            page.setStart((pageNow - 1) * pageSize);
            List<QuessionBank> all = question.findByQuestionNameLikeType(name,page.getStart(),pageSize);
            page.setList((ArrayList<QuessionBank>) all);
            return page;
        }else {
            logger.info("查询题库为空");
            return null;
        }

    }

    //模糊查询笔试题库
    @Override
    public Page<QuessionBank> findByQuestionNameW(String name,Integer pageNow, Integer pageSize) {
        if(adminUtil.isNull(name)){
            Page<QuessionBank> page = new Page<>();
            page.setPageTotalCount(question.queryCountByType("面试"));
            page.setPageNow(pageNow);
            page.setPageSize(pageSize);
            page.setPageTotal(page.getPageTotal());
            page.setStart((pageNow - 1) * pageSize);
            List<QuessionBank> all = question.findByQuestionNameWType(name,page.getStart(),pageSize);
            page.setList((ArrayList<QuessionBank>) all);
            return page;
        }else {
            logger.info("查询题库为空");
            return null;
        }

    }
    //对题库的操作

    @Override
    public List<Interviewers> findInterByName(String name) {
        if(adminUtil.isNull(name)){
            return interviewers.findInterByName(name);
        }else {
            logger.info("该面试者不存在{}",name);
            return null;
        }

    }

    @Override
    public ResponseResult findUserById(int id) {
        User user = userRepository.findById(id);
        if (user == null) {
            return ResponseResult.error("未找到该用户");
        }
        return ResponseResult.success(user);
    }

}
